package com.hskn.hss.module.attend.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.attend.entity.Attend;
import com.hskn.hss.module.attend.mapper.AttendMapper;
import com.hskn.hss.module.attend.service.IAttendService;
import com.hskn.hss.module.attend.vo.CollectListVO;
import com.hskn.hss.module.attendkqjg.entity.AttendKqjg;
import com.hskn.hss.module.attendkqjg.mapper.AttendKqjgMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Service
public class AttendServiceImpl extends ServiceImpl<AttendMapper, Attend> implements IAttendService {

    @Resource
    private AttendMapper attendMapper;
    @Resource
    private AttendKqjgMapper attendKqjgMapper;

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("attendId"))) {
            throw new HssException("参数错误");
        }
        Attend attend = JSON.parseObject(params.toJSONString(), Attend.class);
        attend.setUpdateUid(uid);
        attendMapper.updateById(attend);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Attend attend = JSON.parseObject(params.toJSONString(), Attend.class);
        attend.setCreateUid(uid);
        attendMapper.insert(attend);
    }

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        QueryWrapper<Attend> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Attend::getCreateTime);
        String gridId = params.getString("gridId");
        if (StringUtils.isNotEmpty(gridId)) {
            queryWrapper.lambda().eq(Attend::getGridId, gridId);
        }
        String planName = params.getString("planName");
        if (StringUtils.isNotEmpty(planName)) {
            queryWrapper.lambda().like(Attend::getPlanName, planName);
        }
        String personId = params.getString("personId");
        if (StringUtils.isNotEmpty(personId)) {
            queryWrapper.lambda().eq(Attend::getPersonId, personId);
        }
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(Attend::getDate, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<Attend> attendPage = new Page<>(page, size, true);
        IPage<Attend> iPage = attendMapper.selectPage(attendPage, queryWrapper);
        JSONArray jsonArray = new JSONArray();
        List<Attend> attends = iPage.getRecords();
        for (Attend attend : attends) {
            JSONObject attendJson = JSON.parseObject(JSONObject.toJSONString(attend));
            List<AttendKqjg> kqjgs = attendKqjgMapper.selectList(null);
            attendJson.put("kqjgs", kqjgs);
            jsonArray.add(attendJson);
        }
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("list", jsonArray);
        return json;
    }

    @Override
    public CollectListVO collectList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject res = new JSONObject();
        Long page = (StringUtils.isEmpty(params.getLong("page")) || params.getLong("page") == 0) ? 1 : params.getLong("page");
        Long size = (StringUtils.isEmpty(params.getLong("size")) || params.getLong("size") == 0) ? 10 : params.getLong("size");
        params.put("offset", (page - 1) * size);
        params.put("limit", size);
        List<Map<String, Object>> list = attendMapper.collectList(params);
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> dayStatus = attendKqjgMapper.getStatusDay(map);
            map.put("dayStatus", dayStatus);
        }
        long count = attendMapper.collectListCount(params);
        CollectListVO collectListVO = new CollectListVO();
        collectListVO.setList(list);
        collectListVO.setTotal(count);
        collectListVO.setCurrent(page);
        collectListVO.setPages((int) Math.ceil(count / size));
        collectListVO.setSize(list.size());

        return collectListVO;
    }
}
